package com.gxpt.app.view;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.gxpt.app.AppUtils;
import com.gxpt.app.BaseActivity;
import com.gxpt.app.Constant;
import com.gxpt.app.R;
import com.gxpt.app.bean.confirmorder_bean;
import com.gxpt.app.bean.pubdemand;
import com.utils.T;
import com.utils.logger.Logger;
import com.utils.okhttputils.HttpUtils;
import com.utils.picasso.PicassoUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import okhttp3.Call;

/**
 * Created by dufeng on 2017/8/4.
 */

@ContentView(R.layout.activity_life_need_confirm)
public class Activity_life_need_confirm extends BaseActivity {


    @ViewInject(R.id.toback)
    Button toback;

    @ViewInject(R.id.txt_title)
    TextView txt_title;


    @ViewInject(R.id.put_3)
    TextView put_3;


    @ViewInject(R.id.put_5)
    TextView put_5;


    @ViewInject(R.id.put_1)
    TextView put_1;

    @ViewInject(R.id.put_8)
    TextView put_8;


    @ViewInject(R.id.img_view)
    ImageView img_view;


    @ViewInject(R.id.txt_pic)
    TextView txt_pic;


    @ViewInject(R.id.put_2)
    TextView put_2;

    @ViewInject(R.id.put_4)
    TextView put_4;

    @ViewInject(R.id.put_6)
    TextView put_6;

    @ViewInject(R.id.put_7)
    TextView put_7;

    @ViewInject(R.id.put_9)
    TextView put_9;

    @ViewInject(R.id.put_10)
    TextView put_10;

    @ViewInject(R.id.toSure)
    Button toSure;


    String industry_id;
    String industry_subid;

    String province_id;
    String city_id;

    String bidding_model;//交易模式 0一口价1招标
    String identity;//0需求方1中间商

    String pic_path="";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        initView();
        Bundle b = getIntent().getBundleExtra("DATA");
        if(b!=null){
            put_1.setText(b.getString("put_1"));
            put_2.setText(b.getString("put_2"));
            put_3.setText(b.getString("put_3"));
            put_4.setText(b.getString("put_4"));
            put_5.setText(b.getString("put_5"));
            put_6.setText(b.getString("put_6"));
            put_7.setText(b.getString("put_7"));
            put_8.setText(b.getString("put_8"));
            put_9.setText(b.getString("put_9"));
            put_10.setText(b.getString("put_10"));

            industry_id      = b.getString("industry_id");
            industry_subid   = b.getString("industry_subid");
            province_id      = b.getString("province_id");
            city_id          = b.getString("city_id");
            bidding_model    = b.getString("bidding_model");
            identity         = b.getString("identity");
            pic_path         = b.getString("pic_path");

            PicassoUtils.Picasso_displayImage(context,Constant.IP+pic_path,img_view);

        }
}



    @Override
    public void initView() {
        txt_title.setText(getString(R.string.txt_need_confirm));
    }


    @Override
    public void initData() {

    }



    @Event(value={R.id.toback,R.id.toSure})
    private void getEvent(View view){
        switch(view.getId()){
            case R.id.toback:
                closeActivity();
                break;
            case R.id.toSure:
                toNet_pubdemand();
                break;
        }
    }


    //////NET DO
    StringCallback do_pubdemand = new StringCallback(){


        @Override
        public void onError(Call call, Exception e, int i) {
            Logger.e(e.getMessage());
        }

        @Override
        public void onResponse(String s, int i) {
            if(hud.isShowing()){
                hud.dismiss();
            }
//            Logger.e(s);
            try{
                pubdemand data = new Gson().fromJson(s, pubdemand.class);

                if(data.state !=1 ){
                    T.show(context,data.msg,0);
                }else{

                    if(bidding_model.equals("0")){//一口价
                        toNet_confirmorder(data.data.demand_id);
                    }else{//招标
                        toNet_confirmdemand(data.data.demand_id);

                    }

                }
            }catch (Exception e){
                e.printStackTrace();
                T.show(context,getResources().getString(R.string.alert_again),0);
            }
        }
    };

//    /////NET
    private void toNet_pubdemand(){
        hud.show();
          JSONObject jsonBody=new JSONObject();
        try {
            jsonBody.put("device_number",         AppUtils.get_device_id(context));
            jsonBody.put("device_type",           Constant.device_type);
            jsonBody.put("token",                 AppUtils.get_token(context));
            jsonBody.put("pageName",             "demand");
            jsonBody.put("serviceName",          "pubdemand");

            jsonBody.put("member_id",            AppUtils.get_user_id(context));
            jsonBody.put("industry_id",          industry_id);
            jsonBody.put("industry_subid",       industry_subid);
            jsonBody.put("title",                put_2.getText().toString());
            jsonBody.put("bidding_model",        bidding_model);
            jsonBody.put("project_amount",       put_4.getText().toString());
            jsonBody.put("identity",             identity);
            jsonBody.put("keywords",             put_6.getText().toString());

            jsonBody.put("telphone",             put_7.getText().toString());
            jsonBody.put("province_id",          province_id);
            jsonBody.put("city_id",              city_id);
            jsonBody.put("address",              put_9.getText().toString());
            jsonBody.put("description",          put_10.getText().toString());
            jsonBody.put("file_url",             pic_path);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        Logger.i(jsonBody.toString()+" ==["+Constant.URL_pubdemand+" ]");
        HttpUtils.http_post_string(Constant.URL_pubdemand,jsonBody.toString(),do_pubdemand);
    }


    private void toNet_confirmorder(String demand_id){
        hud.show();
        JSONObject jsonBody=new JSONObject();
        try {
            jsonBody.put("device_number",         AppUtils.get_device_id(context));
            jsonBody.put("device_type",           Constant.device_type);
            jsonBody.put("token",                 AppUtils.get_token(context));
            jsonBody.put("pageName",             "demand");
            jsonBody.put("serviceName",          "confirmorder");

            jsonBody.put("member_id",            AppUtils.get_user_id(context));
            jsonBody.put("demand_id",            demand_id);

        } catch (JSONException e) {
            e.printStackTrace();
        }
//        Logger.i(jsonBody.toString()+" ==["+Constant.URL_confirmorder+" ]");
        HttpUtils.http_post_string(Constant.URL_confirmorder,jsonBody.toString(),do_confirmorder);
    }

    StringCallback do_confirmorder = new StringCallback(){


        @Override
        public void onError(Call call, Exception e, int i) {
            Logger.e(e.getMessage());
        }

        @Override
        public void onResponse(String s, int i) {
            if(hud.isShowing()){
                hud.dismiss();
            }
//            Logger.e(s);
            try{
                confirmorder_bean data = new Gson().fromJson(s, confirmorder_bean.class);

                if(data.state !=1 ){
                    T.show(context,data.msg,0);
                }else{

                    if(bidding_model.equals("0")){//一口价

                        Bundle d = new Bundle();
                        d.putString("order_id",data.data.order_id);
                        d.putString("total_price",data.data.trust_amount);
                        d.putInt("PAY_TYPE",1);
                        jumpActivity(Activity_pay_shopping.class,d,true);
                    }

                }
            }catch (Exception e){
                e.printStackTrace();
                T.show(context,getResources().getString(R.string.alert_again),0);
            }
        }
    };


    //招标确认
    private void toNet_confirmdemand(String demand_id){
        hud.show();
        JSONObject jsonBody=new JSONObject();
        try {
            jsonBody.put("device_number",         AppUtils.get_device_id(context));
            jsonBody.put("device_type",           Constant.device_type);
            jsonBody.put("token",                 AppUtils.get_token(context));
            jsonBody.put("pageName",             "demand");
            jsonBody.put("serviceName",          "confirmdemand");

            jsonBody.put("member_id",            AppUtils.get_user_id(context));
            jsonBody.put("id",                  demand_id);

        } catch (JSONException e) {
            e.printStackTrace();
        }
//        Logger.i(jsonBody.toString()+" ==["+Constant.URL_confirmdemand+" ]");
        HttpUtils.http_post_string(Constant.URL_confirmdemand,jsonBody.toString(),do_confirmdemand);
    }

    StringCallback do_confirmdemand = new StringCallback(){


        @Override
        public void onError(Call call, Exception e, int i) {
            Logger.e(e.getMessage());
        }

        @Override
        public void onResponse(String s, int i) {
            if(hud.isShowing()){
                hud.dismiss();
            }
//            Logger.e(s);
            try{
                confirmorder_bean data = new Gson().fromJson(s, confirmorder_bean.class);

                if(data.state !=1 ){
                    T.show(context,data.msg,0);
                }else{

                    T.show(context,getResources().getString(R.string.alert_success_do),1);
                    closeOtherActivity("Activity_life_need_add");
                    closeActivity();

                }
            }catch (Exception e){
                e.printStackTrace();
                T.show(context,getResources().getString(R.string.alert_again),0);
            }
        }
    };
}
